package com.mingchuang.administrator.mingchuang;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mingchuang.administrator.mingchuang.utils.Constant;
import com.mingchuang.administrator.mingchuang.utils.Util;

/**
 * Created by Administrator on 2016/1/11.
 */
public class LoginActivity extends Activity implements View.OnClickListener {

    private Button loginBtn;
    private Button exitBtn;
    private EditText userText;
    private EditText passText;
    private EditText serverText;

    private Button saveBtn;
    private Button testBtn;
    private String serverName;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        initView();


    }

    private void initView() {
        loginBtn = (Button) findViewById(R.id.login);
        exitBtn = (Button) findViewById(R.id.exit);
        loginBtn.setOnClickListener(this);
        exitBtn.setOnClickListener(this);

        userText = (EditText) findViewById(R.id.user_name);
        passText = (EditText) findViewById(R.id.user_password);

        serverText = (EditText) findViewById(R.id.server_name);

        saveBtn = (Button) findViewById(R.id.server_save);
        testBtn = (Button) findViewById(R.id.server_test);

        saveBtn.setOnClickListener(this);
        testBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:

                checkUserInfo(userText.getText().toString().trim() , passText.getText().toString().trim());
                break;
            case R.id.exit:
                finish();
                break;
            case R.id.server_save://保存自定义主机
                saveServer();
                break;
            case R.id.server_test://连接测试
                serverTest();
                break;
            default:
                break;
        }

    }

    private void serverTest() {

        serverName = serverText.getText().toString().trim();
        if(Util.checkIsNull(serverName)){
            Toast.makeText(this,"请输入服务器地址",Toast.LENGTH_SHORT).show();
        }else{
            HttpUtils http = new HttpUtils();
            http.send(HttpRequest.HttpMethod.GET, "http://116.255.189.224:8080/jfinal_authority_mysql/app/logintest", new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    if(responseInfo.statusCode == 200){
                        Toast.makeText(LoginActivity.this,responseInfo.result,Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(LoginActivity.this,"连接失败，请重试",Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Toast.makeText(LoginActivity.this,"连接失败，请重试",Toast.LENGTH_SHORT).show();

                }
            });





        }



    }


    /**
     * 保存自定义主机
     */

    private void saveServer() {

        serverName = serverText.getText().toString().trim();

        if(Util.checkIsNull(serverName)){

            Toast.makeText(this , "请输入服务器地址",Toast.LENGTH_SHORT).show();

        }else{
            SharedPreferences sp = getSharedPreferences(Constant.SPNAME, Context.MODE_PRIVATE);
            sp.edit().putString("server",serverName).commit();
        }


    }

    private void login() {
            //登录
            Intent intent = new Intent();
            intent.setClass(this,WifiApActivity.class);
            startActivity(intent);
            finish();

    }

    private boolean checkUserInfo(String name, String passWord) {

        if(Util.checkIsNull(name)){
            Toast.makeText(this,"请输入用户名",Toast.LENGTH_SHORT).show();
            return false;
        }

        if(Util.checkIsNull(passWord)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return false;
        }

        RequestParams params = new RequestParams();
        params.addBodyParameter("username",name);
        params.addBodyParameter("password",passWord);
        Log.d("kipeng",name+"/"+passWord);


        //116.255.189.224:8080/jfinal_authority_mysql/app/applogin
        //192.168.1.101/mc/app/applogin
        HttpUtils http = new HttpUtils();

        http.send(HttpRequest.HttpMethod.POST, "http://192.168.1.101/mc/app/applogin" ,params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("kipeng",responseInfo.result);
                Log.d("kipeng",responseInfo.statusCode+"");

                if("200".endsWith(responseInfo.result)){
                    //登陆
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    login();


                }else {
                    Toast.makeText(LoginActivity.this,"账号密码错误",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(LoginActivity.this,"网络异常，请稍后重试",Toast.LENGTH_SHORT).show();
                Log.d("kipeng",s);

            }
        });







        return true;

    }
}
